$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'message/message/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '标题', name: 'title', index: 'title', width: 80 }, 			
			{ label: '内容', name: 'content', index: 'content', },
			{ label: '消息类型', name: 'messageTypeTxt', index: 'message_type',  width: 80, align:'center'},
			{ label: '消息方式', name: 'launchShapeTxt', index: 'launch_shape', width: 80 ,align:'center'},
			{ label: '投放渠道', name: 'launchChannel', index: 'launch_channel', width: 80 ,align:'center'},
			{ label: '是否已读', name: 'readFlag', width: 80, align:'center' },
			{ label: '显示时间', name: 'displayDate', index: 'display_date', width: 80 }, 			
			{ label: '创建时间', name: 'createDate', index: 'create_date', width: 80 }, 			
			{ label: '创建人员', name: 'createUser', index: 'create_user', width: 80 }			
        ],
		viewrecords: true,
        height: 600,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.records",
            page: "page.current",
            total: "page.pages",
            records: "page.total"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		message: {},
        messageTypeEnum:[],
        launchShapeEnum:[],
	},
    watch: {
		// 侦听 showList 变化
        showList: function (newShowList, OldShowList) {
        	if (newShowList === false){ //编辑或者新增时候 加载枚举类
                $.get(baseURL + "message/message/getEnum", function(r){
                    vm.messageTypeEnum = r.enum.messageTypeEnum;
                    vm.launchShapeEnum = r.enum.launchShapeEnum;
                });
			}

        }
    },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
            //window.parent.location.hash = "#modules/service/serviceregister.html";
            //window.parent.vm.goto({hash:'#modules/service/serviceregister.html', orderId:'Z180102102850890'});
            //window.parent.vm.main = "#modules/service/serviceregister.html";
			vm.showList = false;
			vm.title = "新增";
			vm.message = {messageType: null, launchShape: null, delFlag: false, displayDate:null};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.message.id == null ? "message/message/save" : "message/message/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.message),
			    success: function(r){
			    	//console.log(JSON.stringify(vm.message));return;
			    	
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "message/message/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		//标记已读
        markupSelectRead: function () {
            var ids = getSelectedRows();
            $.ajax({
                type: "POST",
                url: baseURL + "message/message/markupRead",
                data: JSON.stringify(ids),
                contentType: "application/json",
                success: function (result) {
                    if (result.code == 0) {
                        alert('操作成功', function(index){
                            $("#jqGrid").trigger("reloadGrid");
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
		getInfo: function(id){
			$.get(baseURL + "message/message/info/"+id, function(r){
                vm.message = r.message;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});